<?php
if ( defined('ABSPATH') )
	include_once ABSPATH . 'config.php';
else
    include_once 'config.php';

include_once ABSPATH."base/posts.php";
include_once ABSPATH."base/people.php";
include_once ABSPATH."base/curl.php";

$sig = "";

if(isset($_REQUEST["id"])) {

	$id = $_REQUEST["id"];
	
}

if(isset($_REQUEST["from"])) {

	$from = $_REQUEST["from"];
	if(strpos($from,"?") == false)
	{
		$req = "?";
	}
	else
	{
		$req="&";
	}
	$req .= 'cmd=verify';
	if(isset($_REQUEST["sig"]))
	{
		$sig = $_REQUEST["sig"];
	}
}

if(isset($_REQUEST["lastupdate"]))
{
	$last_update = $_REQUEST["lastupdate"];
}

if(strlen($sig) > 0)
{
	$person = new Person($id);
    $curl = new cURL(FALSE);
    $url = $from.$req.'&from='.$person->address.'&sig='.$sig;
    $verify_resp = $curl->post($url,'','');

    if($verify_resp == "ok")
    {
    	$contact_id = Person::IDFromAddress($from);
    }
    else
    {
    	die("Request verification failed for ".$url);
    	exit;
    }
}

    $person = new Person($id);

    $name = $person->name;
    $screen_name = $person->screen_name;
    $image = $person->image;
    $email = $person->email;

    //RSS Begin
    $rss = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
    $rss .= '<rss version="2.0">'."\r\n";
    $rss .= '<channel>'."\r\n";
    $rss .= '<title>'.$screen_name.'</title>'."\r\n";
    $rss .= '<link>http://</link>'."\r\n";
    $rss .= '<image>'.$image.'</image>';
    $rss .= '<description></description>'."\r\n";
    $rss .= '<managingEditor><![CDATA['.$email.' ('.$name.')]]></managingEditor>'."\r\n";
    
    
	$post = new Post();

	if(!strlen($contact_id)> 0)
	{
		$contact_id = '';
	}
	
	$rs = $post->Get($id, $contact_id, $last_update);    
    
	if($rs)
	{
	    foreach($rs as $row) {
	        $id = '';
	        $description = '';
	        $pubDate = '';
	        $status = '';
	        $link = '';
	        $guid = '';
	        $source = '';
	        $category = '';
	
	        $id = $row["id"];
	        $pubDate = $row["timestamp"];
	        $pubDate = date('D, d M Y H:i:s O (e)', strtotime($pubDate));
	        $status = $row["status"];
	        $source = $row["source"];
	        $link = $row["link"];
	        $guid = $row["permalink"];
	        
	        //Item       
	        $rss .= '<item>';
	        $rss .= '<title><![CDATA['.$status.']]></title>'."\r\n";
	        $rss .= '<pubDate>'.$pubDate.'</pubDate>'."\r\n";
	        $rss .= '<link><![CDATA['.$link.']]></link>'."\r\n";
	        $rss .= '<guid><![CDATA['.$guid.']]></guid>'."\r\n";
	        $rss .= '<author>'.$email.' ('.$name.')</author>';
	        //$rss .= '<category>'.$category.'</category>';
	        $rss .= '<description><![CDATA[';
	        $rss .= $status.']]></description>'."\r\n";
	        
	        $rss .= '</item>'."\r\n";
	    }
	}

    //End RSS
    $rss .= '</channel>'."\r\n".'</rss>'."\r\n";

    //Write RSS
    echo $rss;
?>